package com.ruoyi.qywx.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.qywx.model.vo.ServeyQuestionVo;
import lombok.Data;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author : hcl
 * @Date : 2023/09/14 11:54:28
 */
@TableName("qy_survey")
@Data
public class Survey implements Serializable {
    private static final long serialVersionUID = 6917302209566854543L;
    //问卷id
    @TableId(type = IdType.AUTO)
    private Long id;
    //团队id
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private Long teamId;
    //问卷标题
    private String title;
    //问卷描述
    private String description;
    //开始时间
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date beginTime;
    //结束时间
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;
    //页面配置
    private String basicConfig;
    //分享配置
    private String shareConfig;
    //签名
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private String sign;
    //是否已删除
    private Boolean isDelete;
    //创建人
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private Long creator;
    //最后一次修改用户
    private Long modifierId;
    //创建时间
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private Date createTime;
    //修改时间
    private Date modifyTime;
    @TableField(exist = false)
    private String questions;

    public static Survey of(List<ServeyQuestionVo> questions, Long userId,
                            Date start, Date end, Long teamId) {
        final List<String> surveyTitle = Arrays.asList("sTitle", "sNotice");
        Survey survey = new Survey();
        survey.teamId = teamId;
        survey.creator = userId;
        survey.beginTime = start;
        survey.endTime = end;
        survey.modifierId = userId;
        survey.sign = IdUtils.simpleUUID();
        questions.stream()
                .filter(q -> surveyTitle.contains(q.getType()))
                .forEach(q -> {
                    if (q.getType().equals(surveyTitle.get(0))) {
                        survey.title = q.getTopic().getTitle();
                    } else {
                        survey.description = q.getTopic().getTitle();
                    }
                });
        return survey;
    }
}

